<script lang="ts" setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { addRuleAPI } from '@/apis/car';
import type { Rule } from '@/types/car';
import { ElMessage } from 'element-plus';

const router = useRouter();
const rule = ref<Rule>({
  id: '', // 假设 id 由后端生成
  ruleNumber: '',
  ruleName: '',
  freeDuration: 0,
  chargeCeiling: 0,
  chargeType: 'duration',
  ruleNameView: ''
});

const addRule = async () => {
  try {
    await addRuleAPI(rule.value);
    ElMessage.success('添加成功');
    router.push({ name: 'CarRule' });
  } catch (error) {
    ElMessage.error('添加失败');
  }
};

const cancelAdd = () => {
  router.push({ name: 'CarRule' });
};
</script>

<template>
  <div class="add-rule-container">
    <h2>增加停车计费规则</h2>
    <el-form :model="rule" label-width="120px">
      <el-form-item label="计费规则编号">
        <el-input v-model="rule.ruleNumber" />
      </el-form-item>
      <el-form-item label="计费规则名称">
        <el-input v-model="rule.ruleName" />
      </el-form-item>
      <el-form-item label="免费时长(分钟)">
        <el-input v-model.number="rule.freeDuration" />
      </el-form-item>
      <el-form-item label="收费上限(元)">
        <el-input v-model.number="rule.chargeCeiling" />
      </el-form-item>
      <el-form-item label="计费方式">
        <el-select v-model="rule.chargeType">
          <el-option label="时长收费" value="duration" />
          <el-option label="按次收费" value="turn" />
          <el-option label="分段收费" value="partition" />
        </el-select>
      </el-form-item>
      <el-form-item label="计费规则">
        <el-input v-model="rule.ruleNameView" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addRule">保存</el-button>
        <el-button @click="cancelAdd">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<style lang="scss" scoped>
.add-rule-container {
  padding: 20px;
  background-color: #fff;
}
</style>